import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/req/club/req_club.dart';
import 'package:flutter_changdian_football/api/req/user/req_user.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class MyPurseController extends GetxController with GetTickerProviderStateMixin {
  static MyPurseController get to => Get.find();

  ScrollController scrollController = ScrollController();
  // 范围 0-255
  int titleAlpha = 0;

  MyPurseController(
    {
      this.isCz = false,
      this.clubId = ''
    }
  );

  final bool isCz;
  final String clubId;

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(() {
      if (scrollController.offset > 110.w) {
        titleAlpha = 255;
      } else if (scrollController.offset <= 0) {
        titleAlpha = 0;
      } else {
        titleAlpha = scrollController.offset * 255 ~/ 110.w;
      }
      update();
    });

    tabController = TabController(length: 2, vsync: this);

    getUserAccountBalance();
  }

  TabController? tabController;

  /// 用户余额
  String userAccountBalance = '0.00';
  bool accountAccess = false;

  // 获取用户余额
  getUserAccountBalance() async {
    try {
      var resp;
      // var resp = await ReqUser.userAccountBalance();
      if (isCz == true) {
        resp = await ReqClub.currentBalance(clubId: clubId);
      } else {
        resp = await ReqUser.userAccountBalance();
      }

      if (resp.data['code'] == '200') {
        if (isCz == true) {
          userAccountBalance = (double.tryParse('${resp.data['data']['balance']}') ?? 0.00).toStringAsFixed(2);
          accountAccess = resp.data['data']['accountAccess'];
        } else {
          userAccountBalance = (double.tryParse('${resp.data['data']}') ?? 0.00).toStringAsFixed(2);
        }
        
        update();
      } else {
        CustomToast.showTextToast(resp.data['msg']);
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }

}